<?php

namespace App\Admin\Controllers;

use App\Services\AqToolService;
use Slowlyo\OwlAdmin\Controllers\AdminController;

/**
 * 集成工具
 *
 * @property AqToolService $service
 */
class AqToolController extends AdminController
{
	protected string $serviceName = AqToolService::class;

	public function list()
	{
		$crud = $this->baseCRUD()
			->filter($this->baseFilter()->body([
				amis()->TextControl('keywords', '工具名称')->size('md')->clearable(1),
				amis()->TextControl('keywords', '显示名称')->size('md')->clearable(1),
			]))
			->headerToolbar([
				$this->createButton('drawer', 'xl'),
				...$this->baseHeaderToolBar()
			])
			->columns([
				amis()->TableColumn('custom_order', '显示顺序')->sortable()->width(100)->align('center'),
				amis()->TableColumn('title', '工具名称')->searchable(1),
				amis()->TableColumn('name', '显示名称')->searchable(1),
				amis()->Operation()->label(admin_trans('admin.actions'))->buttons([
					$this->rowEditButton('drawer', 'xl'),
					$this->rowDeleteButton(),
				])->width('100'),
			]);

		return $this->baseList($crud);
	}

	public function form($isEdit = false)
	{
		return $this->baseForm()->body([
			amis()->NumberControl('custom_order', '显示顺序')->required(1)->value(0)->min(0)->max(999999)->description('越小越靠前'),
			amis()->TextControl('title', '工具名称')->required(1),
			amis()->TextControl('name', '显示名称')->required(1),
			amis()->TextareaControl('prompt', '人设描述')->required(1)->description('替换变量：{form_data}表单内容；{output_text}输出格式')->minRows(6),
			amis()->TextareaControl('output_text', '输出格式')->minRows(6),
			amis()->ComboControl()->label('表单字段')->name('form_rule')->multiple('1')->addable('1')->removable('1')->removableMode('icon')->addBtn([
				'label' => '新增',
				'icon' => 'fa fa-plus',
				'level' => 'primary',
				'size' => 'sm',
			])->items([
				amis()->SelectControl()->name('type')->options([
					[
						'label' => '文本',
						'value' => 'text',
					],
					[
						'label' => '多行文本',
						'value' => 'textarea',
					],
					[
						'label' => '选择',
						'value' => 'select',
					],
				])->width(120)->required()->selectFirst()->placeholder('请选择字段类型'),
				amis()->TextControl()->name('name')->required()->placeholder('请输入字段名称'),
				amis()->TextControl()->name('unit')->placeholder('请输入单位')->disabledOn('type != "text"'),
				amis()->TextControl()->name('remark')->placeholder('请输入备注')->disabledOn('type == "select"'),
				amis()->NumberControl()->name('displayorder')->placeholder('请输入数字')->value('0')->remark([
					'icon' => 'fa fa-question-circle',
					'trigger' => [
						'0' => 'hover',
					],
					'className' => 'Remark--warning',
					'placement' => 'top',
					'content' => '顺序(<font color="red">越小越靠前</font>)',
				]),
				amis()->NumberControl()->name('rows')->placeholder('请输入行数')->value(3)->min(2)->disabledOn('type != "textarea"'),
				amis()->TextControl()->name('options')->placeholder('请输入选项')->disabledOn('type != "select"')->remark([
					'icon' => 'fa fa-question-circle',
					'trigger' => [
						'0' => 'hover',
					],
					'className' => 'Remark--warning',
					'placement' => 'top',
					'content' => '选项(<font color="red">用|分隔</font>)',
				]),
			])->strictMode('1')->syncFields([]),

		]);
	}

	public function detail()
	{
		return $this->baseDetail()->body([
			amis()->TextControl('id', 'ID')->static(),
			amis()->TextControl('title', '工具名称')->static(),
			amis()->TextControl('name', '显示名称')->static(),
			amis()->TextControl('custom_order', '排序')->static(),
			amis()->TextControl('prompt', '人设描述')->static(),
			amis()->TextControl('created_at', admin_trans('admin.created_at'))->static(),
			amis()->TextControl('updated_at', admin_trans('admin.updated_at'))->static(),
		]);
	}
}
